package com.example.dairenkun.neufoodshop.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.adapter.ShopListAdapter;
import com.example.dairenkun.neufoodshop.bean.Shop;
import com.example.dairenkun.neufoodshop.model.ShopModel;
import com.example.dairenkun.neufoodshop.service.ShopService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    public HomeFragment() {
        // Required empty public constructor
    }
    //private Food food;
    private RecyclerView recyclerView;
    private View view=null;
    private Context context;
    String userid;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
             view=inflater.inflate(R.layout.fragment_home, container, false);
             recyclerView=(RecyclerView)view.findViewById(R.id.recycler_id);
             recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
             getUser();
                // Toast.makeText(context,userid,Toast.LENGTH_SHORT).show();

        getData();



        return  view;
    }

    private void getData() {

        //获取网上的数据/
       /* Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://60.205.189.39/ ")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ShopService testService=retrofit.create(ShopService.class);
        Call<List<Shop>> call=testService.getAllShop();*/
        ShopModel shopModel=new ShopModel();
        Call <List<Shop>> call=shopModel.ShopList();

        call.enqueue(new Callback<List<Shop>>() {
            @Override
            public void onResponse(Call<List<Shop>> call, retrofit2.Response<List<Shop>> response)
            {
                List<Shop> shop=response.body();

                //List  mDataList=food.gtTngou();
                recyclerView.setAdapter(new ShopListAdapter(getActivity(),shop));
            }
            @Override
            public void onFailure(Call<List<Shop>> call, Throwable t) {

            }
        });
    }
    private void getUser(){
         context=getActivity();
         SharedPreferences sp = context.getSharedPreferences("UserInfo", context.MODE_PRIVATE);
        userid = sp.getString("user_id", null);
        //Toast.makeText(context,userid,Toast.LENGTH_SHORT).show();

    }

}
